//
//  LssAppAccountTools.swift
//  freeChatting
//
//  Created by mac on 16/4/19.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class LssAppAccountTools: NSObject {
//设置全局访问点
    static let sharedAccountTools:LssAppAccountTools = LssAppAccountTools()
    //定义userAccount保存用户信息
    var userAccount:LssUserAccount?
    
    //MARK:- 是否过期
    var isExpires:Bool{
        let date = userAccount?.expires_date
        if date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            return false
        }else{
            return true
        }
    }
    //MARK:- access_token取值
    var accessToken:String?{
        if userAccount?.access_token != nil{
            if isExpires == false{
                return userAccount?.access_token
            }else{
                return nil
            }
        }else{
            return nil
        }
    }
    //MARK:- 根据access_token取值判断用户是否登录
    var isLogin:Bool{
        if accessToken != nil{
            return true
        }else{
            return false
        }
    }
    //存储路径
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archive")
    //存储数据
    func saveUserAccount(account:LssUserAccount){
        
        userAccount = account
        NSKeyedArchiver.archiveRootObject(account, toFile: path)
    }
    //重写init方法
    override init() {
        super.init()
        userAccount = getUserAccount()
    }
    //读取数据
    func getUserAccount()->LssUserAccount?{
        let result =  NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? LssUserAccount
        return result
    }
}
